//-----------------------------------------------------------------------
// <copyright file="a_FavoriteDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;
using System.Collections.Generic;

namespace ThoiTrang.Data
{    
    public abstract class a_FavoriteDAOBase
    {
        #region Common methods
        public virtual a_Favorite Createa_FavoriteFromReader(IDataReader reader)
        {
            a_Favorite item = new a_Favorite();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) item.ID = (int)reader["ID"];
                if (!reader.IsDBNull(reader.GetOrdinal("UserID"))) item.UserID = (int)reader["UserID"];
                if (!reader.IsDBNull(reader.GetOrdinal("DateAdded"))) item.DateAdded = (DateTime)reader["DateAdded"];
                if (!reader.IsDBNull(reader.GetOrdinal("AlbumID"))) item.AlbumID = (int)reader["AlbumID"];
                if (!reader.IsDBNull(reader.GetOrdinal("FavoriteType"))) item.FavoriteType = (byte)reader["FavoriteType"];
                if (!reader.IsDBNull(reader.GetOrdinal("FavoriteID"))) item.FavoriteID = (int)reader["FavoriteID"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Favorite methods
            
        public virtual void Createa_Favorite(a_Favorite a_Favorite)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Favorites_ins");
                
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, a_Favorite.UserID);
                database.AddInParameter(dbCommand, "@DateAdded", DbType.DateTime, a_Favorite.DateAdded);
                database.AddInParameter(dbCommand, "@AlbumID", DbType.Int32, a_Favorite.AlbumID);
                database.AddInParameter(dbCommand, "@FavoriteType", DbType.Byte, a_Favorite.FavoriteType);
                database.AddInParameter(dbCommand, "@FavoriteID", DbType.Int32, a_Favorite.FavoriteID);
                database.AddOutParameter(dbCommand, "@ID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Favorite.ID = (int)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Createa_FavoriteBySSID methods

        public virtual void Createa_FavoriteBySSID(a_Favorite a_Favorite, string ssid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Favorites_insBySSID");

                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, a_Favorite.UserID);
                database.AddInParameter(dbCommand, "@DateAdded", DbType.DateTime, a_Favorite.DateAdded);
                database.AddInParameter(dbCommand, "@AlbumID", DbType.Int32, a_Favorite.AlbumID);
                database.AddInParameter(dbCommand, "@FavoriteType", DbType.Byte, a_Favorite.FavoriteType);
                database.AddInParameter(dbCommand, "@FavoriteID", DbType.Int32, a_Favorite.FavoriteID);
                database.AddOutParameter(dbCommand, "@ID", DbType.Int32, 0);
                database.AddInParameter(dbCommand, "@SSID", DbType.String, ssid);

                database.ExecuteNonQuery(dbCommand);
                a_Favorite.ID = (int)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Favorite methods
        
        public virtual void Updatea_Favorite(a_Favorite a_Favorite)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Favorites_upd");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, a_Favorite.ID);
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, a_Favorite.UserID);
                database.AddInParameter(dbCommand, "@DateAdded", DbType.DateTime, a_Favorite.DateAdded);
                database.AddInParameter(dbCommand, "@AlbumID", DbType.Int32, a_Favorite.AlbumID);
                database.AddInParameter(dbCommand, "@FavoriteType", DbType.Byte, a_Favorite.FavoriteType);
                database.AddInParameter(dbCommand, "@FavoriteID", DbType.Int32, a_Favorite.FavoriteID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Favorite methods
        public virtual void Deletea_Favorite(int id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Favorites_del");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Deletea_FavoriteImg methods
        public virtual void Deletea_FavoriteImg(int FavId, string ssid, int AlbumId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Favorites_delImg");

                database.AddInParameter(dbCommand, "@FavID", DbType.Int32, FavId);
                database.AddInParameter(dbCommand, "@ssid", DbType.String, ssid);
                database.AddInParameter(dbCommand, "@AlbumID", DbType.Int32, AlbumId);

                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Deletea_Favorite methods
        public virtual void Deletea_FavoriteArtBySSID(int artId, string ssid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Favorites_delArtBySSID");

                database.AddInParameter(dbCommand, "@FavId", DbType.Int32, artId);
                database.AddInParameter(dbCommand, "@SSID", DbType.String, ssid);

                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion


        #region Geta_Favorite methods
        
        public virtual a_Favorite Geta_Favorite(int id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Favorites_sel");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                a_Favorite a_Favorite =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Favorite = Createa_FavoriteFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Favorite;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual List<List<int>> Geta_Favorites(string  ckid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_user_index_sel");

                database.AddInParameter(dbCommand, "@CookieID", DbType.AnsiString, ckid);

                List<List<int>> res = new List<List<int>>();
                int i = 0;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {                        
                        List<int> list = new List<int>();
                        if (!reader.IsDBNull(reader.GetOrdinal("FavoriteType"))) list.Add((int)reader["FavoriteType"]);
                        if (!reader.IsDBNull(reader.GetOrdinal("Counts"))) list.Add((int)reader["Counts"]);
                        if (!reader.IsDBNull(reader.GetOrdinal("NumAlbum"))) list.Add((int)reader["NumAlbum"]);
                        res.Add(list);
                        i++;                       
                    }
                    reader.Close();
                }
                return res;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_FavoriteList methods
        public virtual a_FavoriteCollection Geta_FavoriteList(a_FavoriteColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Favorites_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_FavoriteCollection a_FavoriteCollection = new a_FavoriteCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Favorite a_Favorite = Createa_FavoriteFromReader(reader);
                        a_FavoriteCollection.Add(a_Favorite);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_FavoriteCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_FavoriteCollection Geta_FavoriteList(a_FavoriteColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_FavoriteList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion

        #region Geta_FavoriteListByAlbumID methods
        public virtual a_FavoriteCollection Geta_FavoriteListByAlbumID(int AlbumID, a_FavoriteColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Favorites_selListByAlbumID");

                database.AddInParameter(dbCommand, "@AbId", DbType.Int32, AlbumID);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_FavoriteCollection a_FavoriteCollection = new a_FavoriteCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Favorite a_Favorite = Createa_FavoriteFromReader(reader);
                        a_FavoriteCollection.Add(a_Favorite);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_FavoriteCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_FavoriteCollection Geta_FavoriteListByAlbumID(int AlbumID, a_FavoriteColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_FavoriteListByAlbumID(AlbumID, orderBy, orderDirection, 0, 0, out totalRecords);
        }

        #endregion

        #region Geta_FavoriteListListCatBySSID methods
        public virtual a_FavoriteCollection Geta_FavoriteListListCatBySSID(string ssid, a_FavoriteColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Favorites_selListCatBySSID");

                database.AddInParameter(dbCommand, "@SSID", DbType.String, ssid);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_FavoriteCollection a_FavoriteCollection = new a_FavoriteCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Favorite a_Favorite = Createa_FavoriteFromReader(reader);
                        a_FavoriteCollection.Add(a_Favorite);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_FavoriteCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_FavoriteCollection Geta_FavoriteListListCatBySSID(string ssid, a_FavoriteColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_FavoriteListListCatBySSID(ssid, orderBy, orderDirection, 0, 0, out totalRecords);
        }

        #endregion
    }
}